<?php 

namespace app\common\shop;

use think\Db;
use think\Model;
use think\model\concern\SoftDelete;
use app\common\shop\OrderGoodsModel;
// 商品表
class ShopGoodsModel extends model {
    use SoftDelete;//开启了软删除
    protected  $deleteTime= 'del_time';
    // 完整数据表名
    protected $table = 'shop_goods';
    public $stata = [
        '0'=>'下架',
        '1'=>'上架',
    ];

    public $shoptype = [
        '1'=>'普通商城',
        '2'=>'代金券商城',
        '3'=>'拼团商城',
        '4'=>'代金券',
    ];

    /**
     * [建表]
     * @return
    */
    public static function createTable(){
        return Db::query("Create Table If Not Exists `shop_goods`(
            `id` int(11) unsigned Primary key Auto_Increment,
            `title` varchar(255) NOT NULL COMMENT '商品标题',
            `cateid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '分类id',
            `price` decimal(22,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '价格',
            `total` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '库存',
            `path` text NOT NULL COMMENT '图片',
            `unit` varchar(255) NOT NULL DEFAULT '件',
            `sales` int(11) NOT NULL DEFAULT '0' COMMENT '销量',
            `content` text COMMENT '详情',
            `createtime` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
            `status` int(11) NOT NULL DEFAULT '0' COMMENT '上下架 0下架 1上架',
            `express` decimal(22,2) NOT NULL DEFAULT '0.00' COMMENT '快递费',
            `shoptype` int(10) unsigned NOT NULL DEFAULT '1' COMMENT '商城类型',
            `homecate` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '首页分类id',
            `groupnumber` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '团购人数',
            `del_time` int(11) DEFAULT NULL
        )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商品表'");
    }
 	/**
     * [添加]
     * @return
    */
    public static function add($data){

    	$data['createtime'] = time();
    	return self::insertGetId($data);
    }

    /**
     * [处理数据]
     * @param $input 上传的商品数据
     * @return
     */
    public static function dispose($input,$id='' ){
        $goods_data = [
            'title'=>$input['title'],//标题
            'price'=>$input['price'],//价格
            'total'=>$input['total'],//库存
            'content'=> htmlspecialchars_decode($input['content']),//详情
            'status'=>$input['status'],//上架
            'path' => serialize($input['path']),//图片
            'cateid'=>$input['cateid'],
            'homecate'=>$input['homecate'],
            'shoptype'=>$input['shoptype'],
            'groupnumber'=>$input['groupnumber'],
            'old_prices'=>$input['old_prices'],
            'team_prices'=>$input['team_prices'],
            'credit'=>$input['credit'],
            'sort'=>$input['sort'],
        ];
        if($id){
            self::edit($goods_data,$id);// 修改
        }else{
            $id = self::add($goods_data);//添加
        }
        return $id;
    }

    /**
     * [修改]
     * @return
    */
    public static function edit($data,$id){
        return self::where('id',$id)->update($data);
    }

    /**
     * [商品信息]
     * @return
    */
    public static function getGoods($id,$field='*'){
        return self::where('id',$id)->field($field)->find();
    }
    
    /**
     * [商品列表]
     * @return
    */
    public static function getGoodsList($goodsid = 0){
        return self::where('id','in',$goodsid)->select();
    }

    /**
     * [商品规格等信息]
     * @param
     * @return
     */
    public static function getSpecGoods( $cart =[] ){
        $goods = ShopGoodsModel::getGoods($cart['goodsid'],'id,title,price,team_prices,status,path,total,shoptype,groupnumber,credit');
        $goods['img'] = unserialize($goods['path'])[0];
        if($cart['speclistid']){
            $speclist_data = SpecListModel::getInfo($cart['speclistid']);// 规格信息
            $goods['price'] = $speclist_data['price'];//价格
            $goods['team_prices'] = $speclist_data['team_prices'];//价格
            $title = SpecModel::getSpecTitle($speclist_data['key'],$cart['goodsid']);
            $goods['spec_title'] = $title;
            $goods['spec_title_arr'] = explode(';',$title);// 规格数组（可用于前端自定义样式）
            $goods['total'] = $speclist_data['total'];//库存
            $goods['specid'] = $speclist_data['id'] ;// 规格id
        }
        return $goods;
    }

    /**
     * [检查商品是否存在不同的商城类型]
     * @param
     * @return
     */
    public static function checkGoodsType( $goods ){
        $arr = array_unique(array_column($goods, 'shoptype'));
        if(count($arr) >=2) return false;// 有两个以上
        return true;// 可以购买
    }
}